<?php

namespace App\Kuafu\Shop\Service;

use App\Kuafu\Audit\Enum\AuditEventCodeEnum;
use App\Kuafu\Audit\Service\AuditService;
use App\Kuafu\Base\Enum\DBEnum;
use App\Kuafu\Base\Service\BaseService;
use App\Kuafu\Common\Enum\CodeEnum;
use App\Kuafu\Common\Service\CommonService;
use App\Kuafu\Config\Model\ConfigModel;
use App\Kuafu\Customer\Service\CustomerPlatformService;
use App\Kuafu\Mqtt\Service\MqttService;
use App\Kuafu\Order\Exceptions\OrderException;
use App\Kuafu\Order\Service\OrderPayService;
use App\Kuafu\Pay\Channel\FuYou\FuYouBase;
use App\Kuafu\Pay\Model\PayStreamModel;
use App\Kuafu\Product\Enum\ProductCodeEnum;
use App\Kuafu\Product\Exceptions\ProductException;
use App\Kuafu\Product\Model\Product;
use App\Kuafu\ProductMenu\Enum\ProductMenuCodeEnum;
use App\Kuafu\ProductMenu\Exceptions\ProductMenuException;
use App\Kuafu\ProductMenu\Model\ProductMenu;
use App\Kuafu\Shop\Enum\CodeEnum as ShopEnum;
use App\Kuafu\Shop\Enum\ShopCacheEnum;
use App\Kuafu\Shop\Enum\ShopCodeEnum;
use App\Kuafu\Shop\Exceptions\ShopException;
use App\Kuafu\Shop\Model\AreaModel;
use App\Kuafu\Shop\Model\Shop;
use App\Kuafu\Shop\Model\ShopOperateTargetModel;
use App\Kuafu\Shop\Model\ShopPaymentsModel;
use App\Kuafu\Shop\Model\ShopProductStatus;
use App\Kuafu\User\Enum\CodeEnum as UserCodeEnum;
use App\Kuafu\User\Enum\RoleCodeEnum;
use App\Kuafu\User\Model\Role;
use App\Kuafu\User\Model\User;
use App\Kuafu\User\Model\UserRoleMapping;
use App\Kuafu\User\Service\AuthService;
use App\Kuafu\User\Service\UserRoleService;
use App\Kuafu\User\Service\UserService;
use App\Kuafu\WaiMai\Enum\WaiMaiDBEnum;
use App\Kuafu\WaiMai\Service\WaiMaiShopService;
use App\Kuafu\Wechat\Service\WechatService;
use App\Library\FeiShu\SendFs;
use App\Library\Redis\RedisHelp;
use App\Library\TencentMap\TencentMapHelp;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schema;


class ShopAppletService extends BaseService
{

    /**
     * 获取当前 app list
     *
     * app1 => 老的 appid ....
     * app2 => 新的 appid ....
     *
     * @return mixed
     */
    private static function getAppletVersionList(string $key = '')
    {
        $data = ConfigModel::getConfigByTypeKey('appletVersion', 'list');
        $list = json_decode($data[0]['value'], 1);
        return $list[$key] ?? $list;
    }

    private static function getAppletTemplateList(string $key = '')
    {
        $data = ConfigModel::getConfigByTypeKey('appletTemplate', 'templateList');
        $list = json_decode($data[0]['value'], 1);
        return $list[$key] ?? $list;
    }

    /**
     * 获取老的的小程序详情
     *
     * @return mixed
     */
    public static function getOldAppletVersionInfo()
    {
        return self::getAppletVersionList('app1');
    }

    /**
     * 获取最新的小程序详情
     *
     * @return mixed
     */
    public static function getNowAppletVersionInfo()
    {
        return self::getAppletVersionList('app2');
    }

    /**
     * 获取正在使用老的小程序的门店
     *
     * @return mixed
     */
    private static function getOldShopIdList()
    {
        $data = ConfigModel::getConfigByTypeKey('appletVersion', 'oldShopList');
        $list = json_decode($data[0]['value'], 1);
        return $list;
    }

    /**
     * 判断当前店铺是否使用最新的 appid
     *
     * @param int $shopId
     * @return bool
     */
    public static function checkIsNew(int $shopId)
    {
        $oldList = self::getOldShopIdList();
        if (in_array($shopId, $oldList)) {
            return false;
        }
        return true;
    }

    public static function getAppletInfo(int $shopId)
    {
        $isNew = ShopAppletService::checkIsNew($shopId);
        if ($isNew) {
            $versionInfo = ShopAppletService::getNowAppletVersionInfo();
        } else {
            $versionInfo = ShopAppletService::getOldAppletVersionInfo();
        }
        return $versionInfo;
    }

    public static function getAppletInfoByAppId(string $appid)
    {
        $versionInfo = ShopAppletService::getNowAppletVersionInfo();
        if ($versionInfo['appId'] == $appid) {
            return $versionInfo;
        }
        $versionInfo = ShopAppletService::getOldAppletVersionInfo();
        return $versionInfo;
    }

    public static function getAppletTemplateInfo($shopId)
    {
        $isNew = ShopAppletService::checkIsNew($shopId);
        if ($isNew) {
            $templateInfo = self::getNowAppletTemplateInfo();
        } else {
            $templateInfo = self::getOldAppletTemplateInfo();
        }
        return $templateInfo;
    }

    /**
     * 获取老的的小程序详情
     *
     * @return mixed
     */
    public static function getOldAppletTemplateInfo()
    {
        return self::getAppletTemplateList('app1');

    }

    /**
     * 获取最新的小程序详情
     *
     * @return mixed
     */
    public static function getNowAppletTemplateInfo()
    {
        return self::getAppletTemplateList('app2');
    }

    /**
     * 根据 header 头 获取 小程序 appid
     *
     * @return mixed
     */
    public static function getAppIdByHeaderShopId()
    {
        try {
            $shopId = request()->header('shopId');
            if (empty($shopId)) {
                return self::getOldAppletVersionInfo()['appId'];
            }
            $appInfo = self::getAppletInfo($shopId);
            return $appInfo['appId'];
        } catch (\Exception $exception) {
            Log::channel('order')->error('小程序的 appid 获取异常', [
                'shopId'    => $shopId,
                'exception' => $exception
            ]);

            SendFs::newRobot()->setTitleContent('小程序的 appid 获取异常', '请查看日志')->send();

            // 没办法的办法通过配置文件获取
            return config('pay.wx.mini.app_id');
        }

    }
}

